// $codepro.audit.disable incorrectUseOfEqualsAndCompareTo
/*
 * User.java 
 */
package edu.gatech.oad.user;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import edu.gatech.oad.persistence.PersistentObject;

/**
 * @author cwakamo
 * @version 1.0
 */
public abstract class User extends PersistentObject implements Comparable<User> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String username = "";
	
	private String passwordHash = "";
	
	private boolean suspended = false;
	
	private static final String HASH_ALGORITHM = "SHA1";
	
	private static final String PASSWORD_ENCODING = "ISO-8859-1";
	
	private String firstName = "";
	
	private String lastName = "";
	
	private String emailAddress = "";
	
	private String phoneNumber = "";
	
	private boolean isMale = true;
	
	private Date birthday = new Date();
	
	private int loginCount;
	
	public User(String username) {
		this.username = username;
	}
	
	public User (String username, String password) {
		this.username = username;
		setPassword(password);
	}
	
	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	protected void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the suspended
	 */
	public boolean isSuspended() {
		return suspended;
	}

	/**
	 * @param suspended the suspended to set
	 */
	public void setSuspended(boolean suspended) {
		this.suspended = suspended;
	}
	
	public int getLoginCount() {
		return this.loginCount;
	}
	
	public void setLoginCount(int loginCount) {
		this.loginCount = loginCount;
	}
	
	public void resetLoginCount() {
		this.loginCount = 0;
	}
	
	public void incrementLoginCount() {
		this.loginCount++;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return this.lastName + ", " + this.firstName;
	}
	
	public String getFirstName() {
		return this.firstName;
	}
	
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	
	public String getLastName() {
		return this.lastName;
	}
	
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	/**
	 * @return the emailAddress
	 */
	public String getEmailAddress() {
		return emailAddress;
	}

	/**
	 * @param emailAddress the emailAddress to set
	 */
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	/**
	 * @return the phoneNumber
	 */
	public String getPhoneNumber() {
		return phoneNumber;
	}

	/**
	 * @param phoneNumber the phoneNumber to set
	 */
	protected void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	/**
	 * @return the gender
	 */
	public String getGender() {
		return (this.isMale ? "Male" : "Female");
	}

	protected boolean getIsMale() { // $codepro.audit.disable booleanMethodNamingConvention
		return this.isMale;
	}
	
	/**
	 * 
	 */
	protected void setIsMale(boolean isMale) {
		this.isMale = isMale;
	}

	/**
	 * @return the birthday
	 */
	public Date getBirthday() {
		return birthday;
	}

	/**
	 * @param birthday the birthday to set
	 */
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public final void setPassword(String password) {
		if (password != null) {
			try {
				final MessageDigest digestGenerator = MessageDigest.getInstance(
						HASH_ALGORITHM
						);
				final byte[] digest = digestGenerator.digest(
						password.getBytes(PASSWORD_ENCODING)
						);
				this.passwordHash = new String(digest, PASSWORD_ENCODING);
			} 
			catch (NoSuchAlgorithmException e) {
				e.printStackTrace();
			}
			catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		else {
			this.passwordHash = "";
		}
	}
	
	public final boolean isPassword(String password) {
		try {
			final MessageDigest hashGenerator = MessageDigest.getInstance(
					HASH_ALGORITHM
					);
			final byte[] hash = hashGenerator.digest(
					password.getBytes(PASSWORD_ENCODING)
					);
			return this.passwordHash.equals(new String(hash, PASSWORD_ENCODING));
		}
		catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public int compareTo(User u) {
		int result = this.lastName.compareToIgnoreCase(u.lastName);
		if (result == 0) {
			result = this.firstName.compareToIgnoreCase(u.firstName);
		}
		if (result == 0) {
			result = this.username.compareToIgnoreCase(u.username);
		}
		return result;
	}
	
	public String displayString() {
		return this.getName() + " (" + this.username + ")";
	}
	
	public String toString() {
		return "Username - " + this.username + 
			"; Name - " + this.getName() + 
			"; Email - " + this.emailAddress + 
			"; Phone Number - " + this.phoneNumber + 
			"; Gender - " + this.getGender() + 
			"; Birthday - " + this.birthday + ";"; 
	}
}
